package com.zb.controller;
import com.zb.entity.Product;
import com.zb.util.PageUtil;
import com.zb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;


@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/search")
    @ResponseBody
    public PageUtil<Product> searchProduct(@RequestParam Map<String,Object> param ,
                                                                @RequestParam(required = true, defaultValue = "1") Integer index)throws Exception {
        int size = 2;
        PageUtil<Product> productPage  = productService.queryProductPageByMap(param, index, size);
        return productPage;
    }

    @RequestMapping("/saveOrUpdateProduct")
    @ResponseBody
    public Integer saveOrUpdateProduct(Product product){
        Integer flag=0;
        try {
            if(product.getProductId()==0 || product.getProductId()==0){
                flag=productService.qdtxAddProduct(product);
            }else{
                flag=productService.qdtxModifyProduct(product);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("/getProductById")
    @ResponseBody
    public Product getProductById(Long id){

        Product product=null;
        try {
            product=productService.getProductById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return product;
    }

    @RequestMapping("/batchRemoveProduct")
    @ResponseBody
    public Integer batchRemoveProduct(String ids){
        Integer flag=0;
        try {
            flag=productService.qdtxBatchDeleteProduct(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("/removeProductById")
    @ResponseBody
    public Integer removeProductById(Long id){
        Integer flag=0;
        try {
            flag=productService.qdtxDeleteProductById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
